package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.Movie;
import com.itheima.mapper.MovieMapper;
import com.itheima.service.MovieService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: hsp
 * @Date: 2022/11/17-11-17-0:06
 * @Description: com.itheima.service.impl
 * @version: 1.0.0
 */
@Service
public class MovieServiceImpl implements MovieService {

    @Resource
    private MovieMapper movieMapper;

    /**
     * 查询所有影视列表
     * @return
     */
    @Override
    public List<Movie> findAll() {
        return movieMapper.selectList(null);
    }

    /**
     * 新增影视
     * @param movie
     */
    @Override
    public void save(Movie movie) {
        movieMapper.insert(movie);
    }

    /**
     * 根据影视id查询影视详情
     * @param id
     * @return
     */
    @Override
    public Movie findById(Integer id) {
        return movieMapper.selectById(id);
    }

    /**
     * 修改影视
     * @param movie
     */
    @Override
    public void update(Movie movie) {
        movieMapper.updateById(movie);
    }

    /**
     * 根据影视id删除影视
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        movieMapper.deleteById(id);
    }

    /**
     * 批量删除影视
     * @param ids
     */
    @Transactional
    @Override
    public void deleteByIds(Integer[] ids) {
        if (ids != null && ids.length > 0){
            for (Integer id : ids) {
                movieMapper.deleteById(id);
            }
        }
    }

    /**
     * 根据cid查询分类的影视列表
     * @param cid
     * @return
     */
    @Override
    public List<Movie> findByCid(Integer cid) {
        QueryWrapper<Movie> wrapper = new QueryWrapper<>();
        wrapper.eq("cid", cid);
        return movieMapper.selectList(wrapper);
    }


}
